/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.heathrow.daoImpl2;

import cz.muni.fi.heathrow.dao.AircraftDAO;
import cz.muni.fi.heathrow.entities.Aircraft;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Martin Žák
 */
@Repository
public class AircraftDAOImpl implements AircraftDAO
{
    @PersistenceContext
    private EntityManager manager;
    
    public AircraftDAOImpl()
    {
        
    }

    @Override
    public void create(Aircraft aircraft)
    {
        if (aircraft == null)
            throw new IllegalArgumentException("aircraft");
        if (aircraft.getId() != null)
            throw new IllegalArgumentException("Already persisted");

        manager.persist(aircraft);
        
    }

    @Override
    public Aircraft get(Long id)
    {
        if (id == null)
            throw new IllegalArgumentException("id");

        Aircraft ret = (Aircraft) manager.find(Aircraft.class, id);
        
        return ret;
    }

    @Override
    public void update(Aircraft aircraft)
    {
        if (aircraft == null)
            throw new IllegalArgumentException("aircraft");
        if (aircraft.getId() == null)
            throw new IllegalArgumentException("Instance not persisted");

        manager.merge(aircraft);
    }

    @Override
    public void remove(Aircraft aircraft)
    {
        if (aircraft == null)
            throw new IllegalArgumentException("aircraft");
        if (aircraft.getId() == null)
            throw new IllegalArgumentException("Instance not persisted");

        manager.remove(aircraft);
    }

    @Override
    public List<Aircraft> findAll()
    {
        TypedQuery<Aircraft> q = manager.createQuery("SELECT a FROM Aircraft AS a", Aircraft.class);
        List<Aircraft> res = q.getResultList();

        return res;
    }

    @Override
    public Aircraft findByCode(String code)
    {
        if (code == null || code.equals(""))
            throw new IllegalArgumentException("code null or empty");
        
        TypedQuery<Aircraft> q = manager.createQuery("SELECT a FROM Aircraft AS a WHERE a.code = :code", Aircraft.class);
        q.setParameter("code", code);
        Aircraft res = q.getSingleResult();

        return res;        
    }

    @Override
    public List<Aircraft> findByCodeWild(String code, int limit)
    {
        if (code == null)
            throw new IllegalArgumentException("code null or empty");
        
        TypedQuery<Aircraft> q = manager.createQuery("SELECT a FROM Aircraft AS a WHERE a.code LIKE :code ORDER BY a.code", Aircraft.class);
        q.setParameter("code", "%" + code + "%");
        q.setMaxResults(limit);
        return q.getResultList();       
    }
}
